package com.tomshushu.doc.freedoc.modular.file;

import com.tomshushu.doc.freedoc.core.exception.ServiceException;
import com.tomshushu.doc.freedoc.core.model.response.ResponseData;
import com.tomshushu.doc.freedoc.core.model.response.SuccessResponseData;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: zhouhong
 * @date: 2025-06-19 16:06
 * @version: 1.0
 */
@RestController
@RequestMapping("/doc/file")
public class FileController {

    @Resource
    private FileService fileService;

    @PostMapping("/upload")
    public ResponseData uploadFile(@RequestParam("file") MultipartFile file) {
        FileService.FileUploadResponse response = fileService.uploadFile(file);
        return new SuccessResponseData(response);
    }

    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<Map<String, String>> handleUploadException(ServiceException e) {
        Map<String, String> errorResponse = new HashMap<>();
        errorResponse.put("error", e.getMessage());
        errorResponse.put("timestamp", Instant.now().toString());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
    }

}
